
import axios from '../axiosExtend.js'
import { mediaUrl } from '../api.config.js';
class checkYouTube {
	constructor(out_name, fn){
		let _this = this;
		_this.params = {
			status: true
		},
		_this.account = out_name;
		_this.callBack = fn; // 回调函数
		checkYouTubeAuthor(_this.account, function(flag){
			if(flag) {
				$.message({message: '认证成功！', type:'info'});
				_this.params.status = true;
				_this.callBack(_this.params);
			}else{
				_this.confirmYouTubeUrl();
			}
		});
	}
	confirmYouTubeUrl() {
		let left = (window.outerWidth - 800) / 2 + "px";
		let YouTubeDialog = window.open("", 'Author', "width=800px,height=600px,menubar=no,top=200px,left=" + left);
		let _this = this;
		_confirmYouTubeUrl(_this.account).then(res => {
			if(res.data.errorCode == '0x0') {
				YouTubeDialog.location.href = res.data.result;
				if (YouTubeDialog.closed) { // 用户关闭认证窗口
					_checkYouTubeAuthor(this.account).then(res => {
						if(res.data.errorCode == '0x0') {
							$.message({message: '认证成功！', type:'info'});
							_this.params.status = true;
						}else{
							_this.params.status = false;
						}
					});
					_this.callBack(JSON.stringify(_this.params));
				}
				window.addEventListener('message', function(e){
					// if(e.origin !== "tvunetworks.com") return;
					let data = JSON.parse(e.data)
					_exchangeToken(data).then(res => {
                        if(res.data.errorCode == '0x0') {
							YouTubeDialog.close();
                            _this.callBack(JSON.stringify(_this.params));
                        }else{
							YouTubeDialog.close();
							$.message({message: res.data.errorInfo, type:'info'});
						}
						window.removeEventListener("message", false);
                    });
				}, false);
			}
		});
	}
};
function checkYouTubeAuthor(account, fn) {
	let status = false;
	_checkYouTubeAuthor(account).then(res => {
		if(res.data.errorCode == '0x0') {
			status = true;
		}else if(res.data.errorCode == '81200101'){
			status = false;
		}
		fn(status);
	});
}
function saveAuthInfo(obj, fn) {
	_saveAuthInfo(obj).then(res => {
		let msg = res.data.errorInfo;
		let status = true;
		if(res.data.errorCode == '0x0') {
			status = true;
		}else if(res.data.errorCode == '81200301'){
			status = false;
		}else if(res.data.errorCode == '81200302') {
			status = false;
		}
		fn(status, msg);
	});
}

function _checkYouTubeAuthor(account) {
    let url = mediaUrl() + "youtube/checkYoutubeAuth?account="+account;
    return axios({
        url: url,
        method: 'get'
    })
};

function _confirmYouTubeUrl(account) {
    let url = mediaUrl() + "youtube/clientSecret?account="+account;
    return axios({
        url: url,
        method: 'get'
    })
};

function _exchangeToken(params) {
    let url = mediaUrl() + "youtube/exchangeToken";
    return axios({
        url: url,
        data: params,
        method: 'post'
    });
};
function _saveAuthInfo(params) {
	let url = mediaUrl() + "youtube/authInfo";
	return axios({
        url: url,
        data: params,
        method: 'post'
	});
}
export {
    checkYouTube,
	saveAuthInfo,
	checkYouTubeAuthor,
    _exchangeToken,
    _saveAuthInfo
};